'use strict';
const Controller = require('egg').Controller;
const pump = require('pump');
const fs = require('fs');
/**
 * @controller UploadBookCoverController 上传接口，上传文件保存在public文件夹
 */
class UploadBookCoverController extends Controller {
  async index() {
    /**
     * @description 上传文件
     * @router get /api/uploadBookCover
     */
    const ctx = this.ctx;
    const parts = ctx.multipart({ autoFields: true });
    let files = {};
    let stream;
    while ((stream = await parts()) != null) {
      if (!stream.filename) {
        break;
      }
      const fieldname = stream.fieldname; // file表单的名字\
      // 上传图片的目录
      const dir = await this.service.upload.getUploadFile(stream.filename);
      const target = dir.uploadDir;
      const writeStream = fs.createWriteStream(target);

      await pump(stream, writeStream);

      files = Object.assign(files, {
        [fieldname]: dir.saveDir,
      });
    }
    if (Object.keys(files).length > 0) {
      ctx.body = {
        code: 200,
        message: '图片上传成功',
        data: files,
      };
    } else {
      ctx.body = {
        code: 500,
        message: '图片上传失败',
        data: {},
      };
    }
  }
}

module.exports = UploadBookCoverController;
